package aufgabe6;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class BayesClassifier {
	
	String type;
	int numOfChars = 0;
	Map<Character,Integer> counts = new HashMap<Character, Integer>();
	
	// values as log-values
	Map<Character,Double> f = new HashMap<Character, Double>();
	
	public BayesClassifier(String type) {
		this.type = type;
	}

	public void readFromStdin() {
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(new java.io.BufferedInputStream(System.in), "UTF-8");
		
		if (!scanner.hasNextLine()) {
			System.out.println("No input given.");
			System.exit(1);
		}
		
		while (scanner.hasNextLine()) {
			String s = scanner.nextLine();
			
			if (s.startsWith(">")) continue;
			
			countFromLine(s);
		}
	}
	
	public void countFromLine(String s) {		
		char[] chars = s.toCharArray();
		
		for (char c: chars) {
			numOfChars++;
			
			if (counts.containsKey(c)) {
				counts.put(c, counts.get(c)+1);
			}
			else {
				counts.put(c, 1);
			}
		}
		
		for (char c: counts.keySet()) {
			f.put(c, Math.log(((double)counts.get(c)/numOfChars)));
		}
	}
	
	public void writeToFile(String filename) {
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filename)));

			for (char c: counts.keySet()) {
				bw.write(c+"\t"+f.get(c));
				bw.newLine();
			}        	

			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 2) {
			System.out.println("Usage: BayesClassifier <ext|int>");
			System.exit(1);
		}
		
		BayesClassifier bc = new BayesClassifier(args[0]);
		
		bc.readFromStdin();
		
		bc.writeToFile(args[1]);
	}

}
